<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      v-show="showSearch"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="系列名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入系列名称"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="尺寸名称" prop="size">
        <el-input
          v-model="queryParams.size"
          placeholder="请输入尺寸名称"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
 

      <el-form-item label="创建时间" style="width: 308px">
        <el-date-picker
          v-model="dateRange"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          v-hasPermi="['system:series:add']"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:series:update']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:series:delete']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:series:export']"
          >導出</el-button
        >
      </el-col>
      <el-col :span="1.5">
           <el-button 
            v-if="close"
              type="warning" 
              plain 
              icon="Close"
              @click="handleClose"
           >关闭</el-button>
        </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格数据 -->
    <el-table
      v-loading="loading"
      :data="specificationList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="类别" prop="name" :show-overflow-tooltip="true" >
        <template #default="scope">
               <router-link :to="'/prodect/add/index/' + scope.row.id" class="link-type">
                  <span>{{ scope.row.name }}</span>
               </router-link>
            </template>
      </el-table-column>>  
      <!-- <el-table-column label="尺寸" prop="size" :show-overflow-tooltip="true" />   -->
      <!-- <el-table-column label="色温" prop="colour" :show-overflow-tooltip="true" /> -->
      <el-table-column label="轮播图片" align="center" prop="image" >
        <template #default="scope">
          <image-preview :src="scope.row.url" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" :show-overflow-tooltip="true"  prop="createTime" >
      </el-table-column>
      <el-table-column label="创建人"  prop="nickname"></el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-tooltip
            content="修改"
            placement="top"
           
          >
            <el-button
              link
              type="primary"
              icon="Edit"
              
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:series:update']"
            ></el-button>
          </el-tooltip>
          <el-tooltip
            content="删除"
            placement="top"
          >
            <el-button
              link
              type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:series:delete']"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <pagination
         v-show="total > 0"
         :total="total"
         v-model:page="queryParams.pageNum"
         v-model:limit="queryParams.pageSize"
         @pagination="getList"
      />
    

    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title" v-model="open" width="700px"  append-to-body>
      <el-form ref="addclientele" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="系列名称:" prop="name">
          <el-input v-model="form.name" placeholder="请输入系列名称" />
        </el-form-item>

        <!-- <el-form-item label="尺寸:" prop="size">
          <el-input v-model="form.size" placeholder="请输入尺寸" />
          </el-form-item> -->

          <div v-for="(contact, index) in contacts" :key="index">
          <el-form-item label="轮播图片" prop="imger">
          <image-upload v-model="contact.url"/>
         <!-- <el-select  style="width: 100%;" clearable v-model="contact.colour" placeholder="请输入颜色">
            <el-option
              v-for="item in crm_colour"
              :key="item.value"
              :label="item.label"
              :value="item.label"
            />
          </el-select> -->
        </el-form-item>

        <!-- <el-button
        class="iconfont icon-jiahao"      
              type="primary"
              @click="addContact"
            ></el-button>
            <el-button
              v-show="contacts.length > 1 && index > 0"
               class="iconfont icon-jianhao"      
              type="primary"
              @click="removeContact(index)"
            ></el-button>  -->
      </div>  
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
 
 <script setup name="case">



import{
  addseries,
  listseriesandspecification,
  delseriesandspecification,
  getseries,
  updateseries,
} from "@/api/business/series.js";





import { ref} from "vue";
import { getToken } from "@/utils/auth";


const router = useRouter();
const { proxy } = getCurrentInstance();


const {
  crm_colour,
} = proxy.useDict(
  "crm_colour",
);




//照片的url和色温
const contacts =ref([{
  url: "",
  colour:"",
          
}])
// 初始化格式化的选项数据
const formattedOptions = ref([]);
const specificationList=ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const specificationIds=ref([]);
const crmpictureIds = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);
const loadAll=ref([])
const route = useRoute();



const data = reactive({
  form: {
  
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,

  },
  rules: {
    name: [
      { required: true, message: "系列名称不能为空", trigger: "blur" },
    ],

    // size: [
    //   { required: true, message: "尺寸名称不能为空", trigger: "blur" },
    // ],

  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询列表 */
function getList() {
  loading.value = true;
  listseriesandspecification(queryParams.value,dateRange.value).then(response => {
    specificationList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  }).catch(error => {
    loading.value = false;
  });
};

// 删除图片和颜色
function removeContact(index){

contacts.value.splice(index, 1);
}
// 添加图片和颜色
function  addContact() {
     contacts.value.push({
      url: "",
      colour:""
          
     });
    }



/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 删除按钮操作 */
function handleDelete(row) {
  const id = row.id || ids.value;
  const crmpictureId = row.crmpictureId || crmpictureIds.value ;
  proxy.$modal
  .confirm('是否确认删除为"' + id + '"的数据项?')
    .then(function () {
      return delseriesandspecification(id,crmpictureId);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
/** 導出按钮操作 */
function handleExport() {
  proxy.download(
    "series/export",
    {
      ...queryParams.value,
    },
    `series_${new Date().getTime()}.xlsx`
  );
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  specificationIds.value = selection.map((item) => item.specificationId);
  crmpictureIds.value = selection.map((item) => item.crmpictureId);

  single.value = selection.length != 1;
  multiple.value = !selection.length;
}


function reset() {
  form.value = {
  };
  contacts.value = [{}];
  proxy.resetForm("addclientele");

}
/** 添加单头 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加单头";
}
/** 修改角色 */
function handleUpdate(row) {
  reset();
  const id = row.id || ids.value;
  getseries(id).then(response => {
    form.value = response.data;
    contacts.value=response.data.crmpictureVos;
    open.value = true;
   // 将 row 中的 countiesCities 通过 '-' 分割并复制到 countiesCities 数组中
    title.value = "修改角色";
  })
 
}


/** 提交按钮 */
function submitForm() {
  proxy.$refs["addclientele"].validate((valid) => {
    if (valid) {
// 合并数据
const mergedData = { ...form.value,crmpicture: contacts.value };
      if (form.value.id != undefined) {     
        updateseries(mergedData).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addseries(mergedData).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
//取消按钮
function openscancel(){
  opens.value = false;
}



getList(); 
</script>

<style scoped>
.linkman-input-group {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.linkman-input-group .el-input {
  flex: 1;
  margin-right: 10px;
}
</style>